package com.tongcheng.Controller;

import com.tongcheng.ResultDate;
import com.tongcheng.commons.Contants;
import com.tongcheng.pojo.Coupons;
import com.tongcheng.pojo.Coupons_use_rules;
import com.tongcheng.service.Couponssevice;
import com.tongcheng.utils.ConversionDateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

@Controller
@RequestMapping("/coupons")
@Api(value = "优惠劵",tags = "优惠劵")
public class  CouponsController {
    @Autowired
    private Couponssevice couponssevice;
    @ApiOperation(value = "优惠券添加",notes = "根据提供信息添加优惠券", response = ResultDate.class)
    @PostMapping(value = "/addCoupons",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate addCoupons(@RequestBody Map map){
        map.put("start_time",ConversionDateUtils.conversionDateUtils(map.get("start_time").toString()));
        map.put("end_time",ConversionDateUtils.conversionDateUtils(map.get("end_time").toString()));
        int result2=couponssevice.addCoupons_use_rules(map);
        ResultDate resultDate=new ResultDate();
        if (result2>=0){
            int result1=couponssevice.addCoupons(map);
            if (result1>=0){
                resultDate.setCode(Contants.SUCCESS);
                resultDate.setMsg("添加成功");
            }else {
                resultDate.setCode(Contants.FAILURE);
                resultDate.setMsg("添加成功");
            }
        }else {
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("失败，规则id已存在");
        }
        return resultDate;
    }
    @ApiOperation(value = "优惠券删除",notes = "根据id删除优惠券", response = ResultDate.class)
    @GetMapping(value = "/delCoupons",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate delCoupons( Integer coupons_id){
        int result=couponssevice.delCoupons(coupons_id);
        ResultDate resultDate=new ResultDate();
        if (result>0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("删除成功");

        }else {
            resultDate.setCode(Contants.FAILURE);
            resultDate.setMsg("失败");
        }
        return resultDate;
    }
    @ApiOperation(value = "通过活动id查询该活动的优惠券",notes = "根据活动id     查询优惠券", response = ResultDate.class)
    @GetMapping(value = "/queryCouponsByactid",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate queryCouponsByactid( Integer activity_id){
        List<Map> mapList=couponssevice.queryCoupons(activity_id);

        ResultDate resultDate=new ResultDate();
        if(mapList.size()!=0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        }else {
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查无数据");
        }
        return resultDate;

    }
    @ApiOperation(value = "根据userid查询优惠券",notes = "根据userid查询优惠券", response = ResultDate.class)
    @GetMapping(value = "/queryCouponsByuserid",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultDate queryCouponsByuserid(Integer userId){
        System.out.println(userId+"-------------");
        List<Map> mapList=couponssevice.queryCouponsByuserid(userId);
        ResultDate resultDate=new ResultDate();
        if(mapList.size()!=0){
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查询成功");
            resultDate.setData(mapList);
        }else {
            resultDate.setCode(Contants.SUCCESS);
            resultDate.setMsg("查无数据");
        }
        return resultDate;

    }
//    @ApiOperation(value = "优惠卷使用规则添加",notes = "优惠卷使用规则添加", response = ResultDate.class)
//    @PostMapping(value = "/addCouponsUseRules",produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ResponseBody
//    public ResultDate addCoupons_use_rules(@RequestBody Map map){
//        map.put("start_time",ConversionDateUtils.conversionDateUtils(map.get("start_time").toString()));
//        map.put("end_time",ConversionDateUtils.conversionDateUtils(map.get("end_time").toString()));
//        int result=couponssevice.addCoupons_use_rules(map);
//        ResultDate resultDate=new ResultDate();
//        if(result>0){
//            resultDate.setCode(Contants.SUCCESS);
//            resultDate.setMsg("查询成功");
//        }else {
//            resultDate.setCode(Contants.FAILURE);
//            resultDate.setMsg("查询失败");
//        }
//        return resultDate;
//    }



}
